<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>设备ID绑定</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/css/font.css">
    <link rel="stylesheet" href="/css/xadmin.css">
    <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        body {
            background-image: url("/images/wallhaven.jpg");
            background-repeat: no-repeat;
            background-attachment: fixed;
            background-color: #e4eaf1;
            background-position: center;
        }

        .layui-form-select dl {
            max-height: 224px;
        }
    </style>

</head>

<body>
<div class="layui-fluid" style="background-color:rgba(255,255,255,0.3);min-height: 427px">
    <div class="layui-row">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 10px;">
            <legend class="layui-icon">绑定新的{$devName}设备 &#xe61f;&#xe61f;&#xe61f;</legend>
        </fieldset>
        <form class="layui-form" style="margin-top: 30px">

            <input type="hidden" name="type" value="{$devName}"/>
            <div class="layui-form-item" id="area-picker">
                <label class="layui-form-label">所在地区</label>
                <div class="layui-input-inline layui-btn-fluid">
                    <select name="province" class="province-selector" data-value="" lay-filter="province-1">
                        <option value="">请选择省</option>
                    </select>
                </div>
                <div class="layui-input-inline layui-btn-fluid">
                    <select name="city" class="city-selector" data-value="" lay-filter="city-1">
                        <option value="">请选择市</option>
                    </select>
                </div>
                <div class="layui-input-inline layui-btn-fluid">
                    <select name="county" class="county-selector" data-value="" lay-filter="county-1">
                        <option value="">请选择区</option>
                    </select>
                </div>
            </div>
            <label class="layui-form-label" style="width:80px">用 户 ID</label>
            <div class="layui-input-block">
                <input id="username" type="text" name="username" lay-verify="username" autocomplete="off"
                       placeholder="请输入用户ID" class="layui-input">
            </div>

            <div class="layui-form-item">

                <label class="layui-form-label">设 备 ID</label>
                <div class="layui-input-block">
                    <input id="serialNum" name="serialNum" lay-verify="serialNum" autocomplete="off"
                           placeholder="请输入设备ID" class="layui-input">
                </div>
            </div>
            <!--            提交按钮-->
            <div class="layui-form-item" style="margin-top: 40px">
                <button lay-filter="edit" lay-submit="" class="layui-btn layui-btn-fluid"
                        style="font-family:黑体;font-size:20px;height: 50px;border-radius: 15px">绑定设备
                </button>
            </div>
        </form>
    </div>
</div>

<script>
    //配置插件目录
    layui.config({
        base: '/dist/'
        , version: '1.0'
    });
    layui.use(['form', 'layer', 'layarea', 'jquery'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            $ = layui.jquery;

        var areaDate = '河北省-石家庄市-桥西区';

        layarea.render({
            elem: '#area-picker',
            data: {
                province: '河北省',
                city: '石家庄市',
                county: '桥西区',
            },
            change: function (res) {
                //选择结果
                areaDate = res.province + '-' + res.city + '-' + res.county;
                // console.log(aream);
            }
        });



        //自定义验证规则
        form.verify({
            username: function (value) {
                if (value.length === 0) {
                    return '请输入用户ID！';
                } else if (value.length < 2) {
                    return '长度需大于等于2位'
                }
            },
            serialNum: function (value) {
                if (value.length === 0) {
                    return '请输入设备ID！';
                }
            }

        });

        //监听提交
        form.on('submit(edit)',
            function (data) {
                // console.log(data.field);
                // console.log(aream);
                //使用ajax传递数据
                layer.msg('绑定中……', {time: 600});
                var load = layer.load();
                $.ajax({
                    type: "POST",
                    url: "/index/Tools/device_add",
                    data: {
                        deviceName: data.field.type,
                        uid: data.field.username,
                        serialNum: data.field.serialNum,
                        area: areaDate

                    },
                    success: function (msg) {
                        layer.close(load);
                        parent.parent.layui.notice.remove();
                        if (msg === 'ok') {
                            parent.parent.layui.notice.success('设备ID：' + data.field.serialNum + '与用户：' + data.field.username + '绑定成功！', '操作完成', parent.parent.noticeOpt4);
                            parent.layer.msg('设备ID：' + data.field.serialNum + '与用户：' + data.field.username + '绑定成功！', {
                                icon: 6,
                                time: 1200
                            });
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);

                            //刷新父页面
                            setTimeout(function () {
                                parent.layer.close(index);
                                // parent.location.reload();
                                parent.$("#refresh").click();
                            }, 1200);
                            // parent.location.reload();

                        } else {
                            parent.parent.layui.notice.error(msg, '操作异常', parent.parent.noticeOpt5);
                            layer.alert(msg, {icon: 7});
                        }
                    },
                    error: function () {
                        layer.close(load);
                        parent.parent.layui.notice.remove();
                        parent.parent.layui.notice.error('数据提交失败，请联系后台管理员', '操作异常', parent.parent.noticeOpt5);
                        layer.alert('数据提交失败，请联系后台管理员！', {icon: 2})
                    }
                });

                return false;
            });
    });</script>
</body>
</html>